//
//  AppProPasswordTFView.swift
//  XiaoweHealthy
//
//  Created by Riven on 2022/3/17.
//

import UIKit

class AppProPasswordTFView: AppProTextFieldBaseView {
    
    override var maxCharacterCount: Int {
        24
    }
    
//    override var kAlphaNum: String {
//        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
//    }
    
    var isPwdOk: Bool = false

    override func addSubViews() {
        super.addSubViews()
        
//        titleLb.isEnabled = false
//        button.setImage(R.image.eyeOff(), for: .normal)
//        button.setImage(R.image.eyeOn(), for: .selected)
        
        textField.setPlaceHolderTextColor(placeHolderTextColor)
        
        textField.clearButtonMode = .never
        textField.isSecureTextEntry = true
        textField.keyboardType = .asciiCapable
        textField.delegate = self
        
        titleLb.isHidden = true
        icon.image = R.image.login_smscode_icon()
        
        button.setImage(R.image.eyeOff(), for: .normal)
        button.setImage(R.image.eyeOn(), for: .selected)

        titleLb.text = "密码"
        textField.placeholder = "请输入密码"
    }
    
    override func relayoutSubViews() {
//        relayoutWithTitle()
        relayoutWithIcon()
    }
    
    func relayoutWithIcon() {
        icon.isHidden = false
        titleLb.isHidden = true
        lineView.isHidden = true
        button.isHidden = false
        
//        titleLb.snp.remakeConstraints { make in
//            make.left.equalToSuperview().inset(16)
//            make.width.lessThanOrEqualTo(70)
//            make.centerY.equalToSuperview()
//            make.height.equalTo(24)
//        }
        icon.snp.remakeConstraints { make in
            make.size.equalTo(24)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().inset(20)
        }
        
//        lineView.snp.remakeConstraints { make in
//            make.left.equalTo(icon.snp.right).offset(18)
//            make.centerY.equalTo(icon)
//            make.width.equalTo(1)
//            make.height.equalTo(22)
//        }
        
        button.snp.remakeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.size.equalTo(22)
            make.centerY.equalToSuperview()
        }
        
        textField.snp.remakeConstraints { make in
            make.left.equalTo(icon.snp.right).offset(18)
            make.right.equalTo(button.snp.left).offset(-8)
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
        }
    }
    
    func relayoutWithTitle() {
        titleLb.isHidden = false
        lineView.isHidden = false
        icon.isHidden = true
        
        titleLb.snp.remakeConstraints { make in
            make.left.equalToSuperview().inset(16)
            make.centerY.equalToSuperview()
            make.width.lessThanOrEqualTo(60)
            make.height.equalTo(24)
        }
        
        lineView.snp.remakeConstraints { make in
            make.left.equalTo(titleLb.snp.right).offset(12)
            make.centerY.equalTo(titleLb)
            make.width.equalTo(1)
            make.height.equalTo(18)
        }
        
        button.snp.remakeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.size.equalTo(22)
            make.centerY.equalToSuperview()
        }
        
        textField.snp.remakeConstraints { make in
            make.left.equalTo(lineView.snp.right).offset(12)
            make.right.equalTo(button.snp.left).offset(-8)
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
        }
    }
    
    func relayoutWithoutTitle() {
        titleLb.isHidden = true
        lineView.isHidden = true
        icon.isHidden = true
    
        removeConstraints(titleLb.constraints)
        removeConstraints(lineView.constraints)
        removeConstraints(icon.constraints)
        button.snp.remakeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.size.equalTo(22)
            make.centerY.equalToSuperview()
        }
        
        textField.snp.remakeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(button.snp.left).offset(-8)
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
        }
    }
    
    @objc override func clickButton(sender: UIButton) {
        button.isSelected = !button.isSelected
        if button.isSelected {
            textField.isSecureTextEntry = false
        } else {
            textField.isSecureTextEntry = true
        }
    }
    
    // MARK: - UITextField
    override func textFieldChanged(sender: UITextField) {
//        if textContent.count >= 6 {
        if textContent.count >= 1 {
            isPwdOk = true
        } else {
            isPwdOk = false
        }
        
        tfChangedCallback?(textContent)
    }

}

//extension XWHPasswordTFView {
//    
//    override func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//        if string.isEmpty {
//            return true
//        }
//        
//        let cs = CharacterSet(charactersIn: kAlphaNum).inverted
//        let filtered = string.components(separatedBy: cs).joined(separator: "")
//        if string == filtered {
//            let textCount = textField.text?.count ?? 0
//            if textCount < maxCharacterCount {
//                return true
//            }
//            
//            return false
//        }
//        
//        return false
//    }
//    
//}
